package com.zsk.parse.excel;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * 写csv文件
 * 
 * @author keke
 * @date 2020/05/12
 */
public class WriteCSV {

    public static void writeTxtExt(String contect, String filePath) {
        File file = new java.io.File(filePath);
        File dir = file.getParentFile();
        if (!dir.isDirectory()) {
            dir.mkdirs();
        }
        FileWriter fWriter = null;
        try {
            fWriter = new FileWriter(file);
            fWriter.write("");
            fWriter.write(contect);
            fWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fWriter != null)
                try {
                    fWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }

    public static void writeTxt(String content, String filePath) {
        FileOutputStream fos = null;
        PrintWriter out = null;
        try {
            fos = new FileOutputStream(filePath);
            OutputStreamWriter rWriter = new OutputStreamWriter(fos, "GB2312");
            out = new PrintWriter(new BufferedWriter(rWriter));
            out.write(content);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (out != null)
                    out.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        sb.append("\t测试1,\t测试2,\t测试3,\t测试4\n");
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 4; j++) {
                sb.append("\t" + UUID.randomUUID().toString().subSequence(0, 7) + ",");
            }
            sb.append("\n");
        }
        writeTxt(sb.toString(), "/p-test.csv");
    }
}
